package org.openkoala.koala.wizard.wizardpage.component;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;
import org.openkoala.koala.createvo.TypeDef;

/**
 * 添加功能模块对话框
 * 
 * @author zyb
 * 
 */
@Deprecated
public class AddFunctionModuleDialog extends Dialog {

	private static final String LABEL_TEXT = "功能依赖列表";
	private static final String OK_BTN_TEXT = "确定";
	private static final String CANCEL_BTN_TEXT = "取消";

	protected Object result;
	protected Shell shell;
	private Table table;
	
	// 用于收集TableItem选中后的text
	private Set<String> selectedItems = new HashSet<String>();

	private AddModuleDialog addModuleDialog;

	// 功能依赖
	private List<String> dependencyList = new ArrayList<String>();

	private Text descriptionTxt;
	
	private Map<String, String> functions;
	

	/**
	 * Create the dialog.
	 * 
	 * @param parent
	 * @param style
	 * @wbp.parser.constructor
	 */
	public AddFunctionModuleDialog(Shell parent, int style, AddModuleDialog addModuleDialog) {
		super(parent, style);
		this.addModuleDialog = addModuleDialog;
	}

	/**
	 * Open the dialog.
	 * 
	 * @return the result
	 */
	public Object open() {
		createContents();
		initControlState();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * 初始化控件状态
	 */
	private void initControlState() {
		if (addModuleDialog != null) {
			selectedItems.addAll(addModuleDialog.getFunList());
			dependencyList.addAll(addModuleDialog.getFunList());
			for (String dependency : dependencyList) {
				for (int i = 0; i < table.getItemCount(); i++) {
					if (dependency.equalsIgnoreCase(table.getItem(i).getText())) {
						table.getItem(i).setChecked(true);
						continue;
					}
				}
			}
		}
	}

	/**
	 * Create contents of the dialog.
	 */
	private void createContents() {
		shell = new Shell(getParent(), SWT.CLOSE | SWT.MAX | SWT.RESIZE | SWT.TITLE);
		shell.setSize(452, 436);
		shell.setLayout(new GridLayout(2, false));

		GridData gd = new GridData(GridData.FILL_HORIZONTAL);
		gd.horizontalAlignment = GridData.HORIZONTAL_ALIGN_END;

		Label label = new Label(shell, SWT.NONE);
		label.setText(LABEL_TEXT);
		gd.horizontalSpan = 2;
		label.setLayoutData(gd);

		table = new Table(shell, SWT.MULTI | SWT.CHECK | SWT.BORDER);
		gd = new GridData(SWT.FILL, SWT.CENTER, false, false, 1, 1);
		gd.widthHint = 189;
		gd.heightHint = 307;
		table.setLayoutData(gd);

		functions = TypeDef.getInstance().getFunctions(addModuleDialog.getModuleType());
		for (Entry<String, String> entry : functions.entrySet()) {
			TableItem item = new TableItem(table, SWT.NONE);
			item.setText(entry.getKey());
		}
		
		descriptionTxt = new Text(shell, SWT.BORDER | SWT.READ_ONLY);
		gd = new GridData(GridData.VERTICAL_ALIGN_BEGINNING);
		gd.horizontalAlignment = SWT.FILL;
		gd.verticalAlignment = SWT.FILL;
		
		descriptionTxt.setLayoutData(gd);

		Composite composite = new Composite(shell, SWT.NONE);
		composite.setLayout(new RowLayout());

		gd = new GridData();
		gd.horizontalSpan = 2;
		gd.horizontalAlignment = SWT.FILL;
		gd.horizontalAlignment = GridData.END;
		composite.setLayoutData(gd);

		Button okBtn = new Button(composite, SWT.PUSH);
		okBtn.setLayoutData(new RowData(84, SWT.DEFAULT));
		okBtn.setText(OK_BTN_TEXT);

		Button cancelBtn = new Button(composite, SWT.PUSH);
		cancelBtn.setLayoutData(new RowData(84, SWT.DEFAULT));
		cancelBtn.setText(CANCEL_BTN_TEXT);

		cancelBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				AddFunctionModuleDialog.this.shell.dispose();
			}
		});

		table.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				TableItem item = (TableItem) e.item;
//				selectedItems.addAll(dependencyList);
				if (item.getChecked()) {
					// 如果被选中则放到List中
					selectedItems.add(item.getText());
					return;
				}
				// 取消选中后则从List中删除
				selectedItems.remove(item.getText());
			}
		});

		okBtn.addSelectionListener(new SelectionAdapter() {
			@Override
			public void widgetSelected(SelectionEvent e) {
				if (addModuleDialog != null) {
//					addModuleDialog.updateFunctionList(CollectionUtils.asList(selectedItems));
					shell.dispose();
				}
			}
		});
		
		table.addListener(SWT.MouseDown, tableListener);
	}
	
	// Table事件监听器
	private Listener tableListener = new Listener() {
		public void handleEvent(Event event) {
			switch (event.type) {
			case SWT.Dispose:
			case SWT.KeyDown:
			case SWT.MouseDown: { // 鼠标按下事件
				TableItem item = table.getItem(new Point(event.x, event.y));
				if (item != null) {
					descriptionTxt.setText(functions.get(item.getText()));
				}
				break;
			}
			}
		}
	};

}
